package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import com.shiguo365.market.R;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IForgetView;
import com.shiguo365.market.presenter.implement.ForgetPresenter;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetActivity extends BaseActivity implements IForgetView {

    ForgetPresenter forgetPresenter;

    private boolean result = true;
    private int time = 60;
    private int count = 0;

    @BindView(R.id.editTextForgetUserName) EditText mEdtforgetUserName;
    @BindView(R.id.buttonSendForgetSMSCode) Button mBtnSendForgetSMSCode;
    @BindView(R.id.editTextForgetUserPassword) EditText mEdtForgetUserPassword;
    @BindView(R.id.editTextForgetSMSCode) EditText mEdtForgetSMSCode;
    @BindView(R.id.editTextForgetUserPasswordAgain) EditText mEdtForgetUserPasswordAgain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        this.setTitle("重置密码");

        ButterKnife.bind(this);

        forgetPresenter = new ForgetPresenter(this,this);
    }

    @OnClick(R.id.buttonForget)
    public void forget(){

        String username = mEdtforgetUserName.getText().toString();
        String code = mEdtForgetSMSCode.getText().toString();
        String pwd = mEdtForgetUserPassword.getText().toString();
        String pwd_cfg = mEdtForgetUserPasswordAgain.getText().toString();

        if("".equals(username)){
            this.showForgetMessages("手机号不能为空");
            return;
        }
        if("".equals(code)){
            this.showForgetMessages("验证码不能为空");
            return;
        }
        if("".equals(pwd)){
            this.showForgetMessages("登录密码不能为空");
            return;
        }
        if("".equals(pwd_cfg)){
            this.showForgetMessages("确认登录密码不能为空");
            return;
        }
        if(!pwd.equals(pwd_cfg)){
            this.showForgetMessages("登录密码和确认登录密码不一致");
            return;
        }
        forgetPresenter.forget(username,code,pwd);

    }

    @OnClick(R.id.buttonSendForgetSMSCode)
    public void forgetsmscheck(){
        String mobile=mEdtforgetUserName.getText().toString();

        if("".equals(mobile)){
            this.showForgetMessages("手机号不能为空");
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (result) {
                    time--;
                    try {
                        Thread.sleep(1000);
                        mBtnSendForgetSMSCode.post(new Runnable() {
                            @Override
                            public void run() {
                                mBtnSendForgetSMSCode.setText(time + "s后重新获取");
                                mBtnSendForgetSMSCode.setClickable(false);
                            }
                        });
                        if (time <= 1) {
                            count=0;
                            result = false;
                            mBtnSendForgetSMSCode.post(new Runnable() {
                                @Override
                                public void run() {
                                    mBtnSendForgetSMSCode.setText("获取验证码");
                                    mBtnSendForgetSMSCode.setClickable(true);
                                }
                            });
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                result = true;
                time = 60;
            }
        }).start();

        forgetPresenter.smscheck(mobile);
    }

    @Override
    public void showForgetMessages(String msg) {
        ToastUtils.with(this).show(msg);
    }

    @Override
    public void forgetSuccess() {

        Intent intent = new Intent(ForgetActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
